package org.example.m6;

import java.util.Random;
import java.util.Scanner;
public class BasketballCompetition {
    static String[] teams = {"队伍A", "队伍B", "队伍C", "队伍D", "队伍E", "队伍F", "队伍G", "队伍H", "队伍9", "队伍10"};
    static int[] scores = new int[teams.length];  //保存各组得分
    static int[] matches = new int[teams.length];  //记录比赛场数
    static int[] wincounts = new int[teams.length];  //记录获胜场数

    public static void main(String[] args) {
        //安排赛程,即各队伍对阵情况
        matchSchedule();
        //记录各队伍得分
        matchRecode();
        //比赛结束，输出得队伍得分及获胜场数
        result();
    }


    private static void matchRecode() {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < teams.length; i++) {
            for (int j = i + 1; j < teams.length; j++) {
                System.out.println("请输入第" + (i * teams.length + j + 1) + "场比赛的结果：");
                // 随机选择获胜队伍
                int winnerIndex = random.nextInt(2) == 0 ? i : j; // 随机选0或1，分别对应队伍i或队伍j作为赢家
                String winner = teams[winnerIndex];
                int winnerScore = 10; // 获胜队伍得10分
                int loserScore = 0;   // 失败队伍得0分
                int loserIndex = winnerIndex == i ? j : i; // 另一队伍为输家

                System.out.printf("获胜队伍：%-15s 得分：%d%n", winner, winnerScore);

                scores[winnerIndex] += winnerScore;
                scores[loserIndex] += loserScore;
                matches[winnerIndex]++;
                matches[loserIndex]++;
                wincounts[winnerIndex]++;
            }
        }
    }




    //赛程安排方法
    private static void matchSchedule() {
        System.out.println("赛程安排：");
        for (int i = 0; i < teams.length; i++) {
            for (int j = i + 1; j < teams.length; j++) {
                System.out.printf("第%d场比赛：%-15s vs %-15s%n", i * teams.length + j + 1, teams[i], teams[j]);
            }
        }
    }

    //判断队伍是否存在方法
    private static boolean isValidTeamName(String winner) {
        for (String t : teams) {
            if (t.equals(winner)) {
                return true;
            }
        }
        return false;
    }
    //检验分数是否合法方法
    private static boolean isValidScore(int winnerScore) {
        return winnerScore >= 0;
    }
    //通过队名获取队伍在数组中的索引方法
    private static int getTeamIndex(String winner) {
        for (int i = 0; i < teams.length; i++) {
            if (teams[i].equals(winner)) {
                return i;
            }
        }
        return -1;
    }
    //显示比赛结果方法要求输出每个队伍的总得分，进行了多少场比赛，获胜场次
    private static void result() {
        System.out.println("比赛结果：");
        for (int i = 0; i < teams.length; i++) {
            System.out.println(teams[i] + " 总得分: " + scores[i] + ", 比赛场次: " + matches[i] + ", 获胜场次: " + wincounts[i]);
        }
    }


}










